﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NotificationServer.DataAccessLayer.DAO;
using System.Data;
using NotificationServer.ServiceLayer.Data;

namespace NotificationServer.BusinessLayer
{
    public class DeviceBO
    {
        private DeviceDAO _deviceDAO;
        private int _maximumNameSize = 200;
        private int _maximumDeviceTypeNameSize = 100;
        private int _maximumRegistrationIdSize = 256;

        public DeviceBO()
        {
            _deviceDAO = new DeviceDAO();
        }

        /// <summary>
        /// If the validation of params success it will register the device
        /// </summary>
        /// <param name="name">Displayname of device</param>
        /// <param name="device_type">Which operatingsystem does the device have</param>
        /// <param name="registration_id">the GCM registration_id</param>
        /// <returns>The inserted GUID or NULL if the validation of params fail</returns>
        public string RegisterDevice(string name, string device_type, string registration_id)
        {

            if (name == null || name.Length > _maximumNameSize && name.Length == 0)
                // TODO: Error Handling 
                return null;
            if (device_type == null || device_type.Length > _maximumDeviceTypeNameSize && device_type.Length == 0)
                // TODO: Error Handling 
                return null;
            if (name == null || name.Length > _maximumRegistrationIdSize && registration_id.Length == 0)
                // TODO: Error Handling 
                return null;

            //Check if the device already exists in database
            DataTable da = _deviceDAO.SearchDevice(name, registration_id);

            if (da.Rows.Count > 0)
                // TODO: Error Handling
                return null;

            Guid guid = Guid.NewGuid();

            if (!_deviceDAO.RegisterDevice(guid, name, device_type, registration_id))
                return null;

            return guid.ToString();
        }

        /// <summary>
        /// If the validation of params success it will update the device with given parameters
        /// </summary>
        /// <returns></returns>
        public int UpdateDevice(string guid, string name, string registration_id)
        {
            if (name == null || name.Length > _maximumNameSize && name.Length == 0)
                // TODO: Error Handling 
                return 0;
            if (guid == null || guid.Length > _maximumDeviceTypeNameSize && guid.Length == 0)
                // TODO: Error Handling 
                return 0;
            if (name == null || name.Length > _maximumRegistrationIdSize && registration_id.Length == 0)
                // TODO: Error Handling 
                return 0;
            
            Guid guidObject;
            try
            {
                guidObject = new Guid(guid);
            }
            catch (FormatException e)
            {
                throw;
            }
            
            return _deviceDAO.UpdateDevice(guidObject, name, registration_id);
        }

        public int UnregisterDevice(string guid)
        {            
            if (guid == null || guid.Length > _maximumDeviceTypeNameSize && guid.Length == 0)
                // TODO: Error Handling 
                return 0;            

            Guid guidObject;
            try
            {
                guidObject = new Guid(guid);
            }
            catch (FormatException e)
            {
                throw;
            }

            return _deviceDAO.UnregisterDevice(guidObject);
        }

        public Device[] GetDevices()
        {
            DataTable da = _deviceDAO.GetDevices();

            int nDevices = da.Rows.Count;
            Device[] devices = new Device[nDevices];

            int rowsCounter = 0;
            foreach (DataRow row in da.Rows)
            {
                devices[rowsCounter] = new Device();
                devices[rowsCounter].Id = (Guid)row["id"];
                devices[rowsCounter].Name = row["name"].ToString();
                devices[rowsCounter].Device_type = row["device_type"].ToString();
                devices[rowsCounter].Registration_id = row["registration_id"].ToString();
                rowsCounter++;
            }

            return devices;
        }        
    }
}